var cpt=0;
$(window).ready(function(){
	$("table").paginationcustom();
	$("#tablesorter").tablesorter();
	if(window.location.pathname == "/AccueilConnecter.php")
	{
		$("#accueil").addClass("active");
		//$("span").hide();
	}
	if(window.location.pathname == "/AjoutTicket.php")
	{
		$("#ajoutTicket").addClass("active");
		$("span").hide();
	}
	if(window.location.pathname == "/gestProblematique.php")
	{
		$("#problematique").addClass("active");
	}
	if(window.location.pathname == "/ConsultationTicket.php" )
	{
		$("#consultation").addClass("active");
	}
	if(window.location.pathname == "/gestTech.php")
	{
		$("#tech").addClass("active");
	}
	if(window.location.pathname == "/Statistiques.php")
	{
		$("#stats").addClass("active");
	}

	$("#graphAppelsTemps").hide();
	$("#camenbertProblematique").hide();

	listenerDetails();

	$("#dateDebut").hide();
	$("#dateFin").hide();
	$("#btn_tri").hide();
	$( "#dateDebut" ).datepicker({
		dateFormat: "yy-mm-dd"	
	});
	$( "#dateFin" ).datepicker({
		dateFormat: "yy-mm-dd"
	});
	$( "#dateDebut2" ).datepicker({
		dateFormat: "yy-mm-dd"
	});
	$( "#dateFin2" ).datepicker({
		dateFormat: "yy-mm-dd"
	});

	if($(".problematique").length && $(".problematique_secondaire").length){
		gestion_problematique();
		modification_pb();
		set_actif();
		ajout_problematique()
	}
	if($('#TempsNbAppels').length){
		chartdisplayer()
		$(window).scroll(function(event) {
			chartdisplayer()
		});
	}
//	afficheGraphDuree();
//	afficheGraphCamenbert();

});
function chartdisplayer(){
	var scroll = $(window).scrollTop();
	var divtps = $('#TempsNbAppels').position().top;
	var screenHeight =screen.height-100;
	var total=divtps-screenHeight-scroll
	if( total<0 && cpt==0){
		cpt++;
		envoiDonneeTotal();
		drawpie();
	}
}
function listenerDetails(){
	$(".cache").hide();

	$(".btn_modifier").click(function(){
		id = $(this).parent().find('.id').val()
		$("#POST"+id).submit();

	});
	$(".btn_oeil").click(function(){
		id = $(this).parent().find('.id').val()
		$("#POST"+id).submit();

	});

	$('.visible td').click(function(){
		if($(this).find(".btn_modifier").length==0){
			$(this).parent().next('.cache').toggle(500);
		}
	});
	$('.comreduit').mouseover(function() {
		var item=$(this).find(".comComplet");
		$(this).popover({
			title : "Commentaire complet",
			placement : "top",
			content : item.val(),
			container : $(this),
		})
		$(this).popover('show')
	});
	$(".comreduit").mouseleave(function() {
		//$(this).parent().attr("colspan","5");
		$(this).popover('destroy')
	});


}

function chargerDetails(){

}
function myFunction(index){
	value=$('#demo'+index).text();
	item=$('#demo'+index);
	if(value === "Oui"){
		item.text("Non");
		$('#actifBool'+index).val(0);		
		return;
	}
	if($('#demo'+index).text()==="Non"){
		item.text("Oui");
		$('#actifBool'+index).val(1);
	}
}

function disablePriorite() {
	select = document.getElementById("Cloture");
	choice = select.selectedIndex;
	priorite =  document.getElementById("prioritedisable");
	choice = select.value;
	console.log(choice);
	if(choice == 1){
		priorite.disabled = true;
		priorite.value = 1;
		var option = document.createElement("option");
		option.text = "Cloturer";
		option.value = "";
		option.selected = true;
		priorite.add(option);
	}
	if(choice != 1){
		priorite.disabled = false
		priorite.value = 1;
		priorite.remove(3);
	}

	else{
		if(choice == 1){
			priorite.disabled = true;
		}
		if(choice != 1){
			priorite.disabled = false
			priorite.value = 1;
		}

	}
}

function afficherCateg() {
	LB = document.getElementById("Categ");
	LB.style.display = "block";
	return true;
}

function cacherCateg() {
	LB = document.getElementById("Categ");
	LB.style.display = "none";
	return true;
}

function afficherSousType() {
	LB = document . getElementById ( "probSec" );
	LB.style.display = "block";
	var value = document.getElementById("probPrinc").value;
	data= {"idEnvoiAjax": value};
	$.ajax({url:"retourAjax/AfficheSousProbl.php",
		data:data,
		type:"POST"
	}).done(function(data){

		data=JSON.parse(data);
		$("#probSec").empty();
		var option = document.createElement("option");
		LB.add(option);
		for(x=0; x<data.length;x++){
			var option = document.createElement("option");
			option.text = utf8_decode(data[x].nature);
			option.value = data[x].id;
			LB.add(option);
		}

	});
}
function supprimerticket(id){
	if (confirm("êtes-vous sur de vouloir supprimer cet appel ?")) {
		data= {"idEnvoiAjax": id};
		console.log(data);
		$.ajax({url:"retourAjax/suppressionTicket.php",
			data:data,
			type:"POST"
		}).done(function(data){
			alert(data);
			location.reload();
		});
	}
	else {
	}

}
function utf8_decode(str_data) {
	//  discuss at: http://phpjs.org/functions/utf8_decode/
	// original by: Webtoolkit.info (http://www.webtoolkit.info/)
	//    input by: Aman Gupta
	//    input by: Brett Zamir (http://brett-zamir.me)
	// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// improved by: Norman "zEh" Fuchs
	// bugfixed by: hitwork
	// bugfixed by: Onno Marsman
	// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// bugfixed by: kirilloid
	//   example 1: utf8_decode('Kevin van Zonneveld');
	//   returns 1: 'Kevin van Zonneveld'

	var tmp_arr = [],
	i = 0,
	ac = 0,
	c1 = 0,
	c2 = 0,
	c3 = 0,
	c4 = 0;

	str_data += '';

	while (i < str_data.length) {
		c1 = str_data.charCodeAt(i);
		if (c1 <= 191) {
			tmp_arr[ac++] = String.fromCharCode(c1);
			i++;
		} else if (c1 <= 223) {
			c2 = str_data.charCodeAt(i + 1);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
			i += 2;
		} else if (c1 <= 239) {
			// http://en.wikipedia.org/wiki/UTF-8#Codepage_layout
			c2 = str_data.charCodeAt(i + 1);
			c3 = str_data.charCodeAt(i + 2);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		} else {
			c2 = str_data.charCodeAt(i + 1);
			c3 = str_data.charCodeAt(i + 2);
			c4 = str_data.charCodeAt(i + 3);
			c1 = ((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63);
			c1 -= 0x10000;
			tmp_arr[ac++] = String.fromCharCode(0xD800 | ((c1 >> 10) & 0x3FF));
			tmp_arr[ac++] = String.fromCharCode(0xDC00 | (c1 & 0x3FF));
			i += 4;
		}
	}


	return tmp_arr.join('');
}
function gestion_problematique(){
	$(".problematique_secondaire div").hide();
	name=$(".problematique").find("input").first().attr("name");
	$("."+name).wrapAll("<div class='wrapme'></div>");
	$(".wrapme").append("<i class='fa fa-plus-square fa-3 addpbsec'></i>");
	$("."+name).show();
	$(".problematique_secondaire").append("<input type='hidden' name='idpb' value="+name+">");
	var childPos = $(".problematique_secondaire .wrapme").first().position();
	var parentPos = $(".problematique_secondaire .wrapme").first().parent().position();
	var height= $(".problematique h2").outerHeight();
	var top= 65;
	console.log(top);
	$(".problematique").append("<i class='fa fa-plus-square fa-3 addpb'></i>");
	$(".problematique div").click(function(){
		$(".problematique_secondaire input[name='idpb']").remove();
		$(".wrapme div").first().unwrap();
		$(".wrapme i").first().unwrap();
		$(".addpbsec").remove();
		$("."+$(this).attr("id")).wrapAll("<div class='wrapme'></div>");
		$(".wrapme").append("<i class='fa fa-plus-square fa-3 addpbsec'></i>");
		$(".problematique_secondaire div").hide();
		if(!$("."+$(this).attr("id")).length){
			$(".problematique_secondaire").append("<i class='fa fa-plus-square fa-3 addpbsec'></i>");
			$(".addpbsec").wrapAll("<div class='wrapme'></div>");
		}
		$(".wrapme").attr("style","position:absolute; top:"+top+"px");
		$("."+$(this).attr("id")).show();
		$(".problematique_secondaire").append("<input type='hidden' name='idpb' value="+$(this).attr("id")+">");
		ajout_problematique()
	});
}
function modification_pb(){
	$('.fa.fa-check').unbind("click");
	$('.fa.fa-pencil-square-o').unbind("click");
	$('.fa.fa-pencil-square-o').click(function(){
		item=$(this).parent().find("span");
		item.attr('contenteditable',"");
		$(this).attr("class","fa fa-check");
		$('.fa.fa-check').click(function(){
			item=$(this).parent().find("span").text();
			if($(this).parent().hasClass('categorie_secondaire')){
				id=$(this).parent().attr("class").split(" ")[2];
			}else{
				id=$(this).parent().attr("id");
			}
			$(this).appelDynamique({"type":"modif_pb_princ", "idpb":id, "data":item},"./retourAjax/receptionAjax.php", "notify");
			$(this).attr("class","fa fa-pencil-square-o");
			modification_pb();
		});
	});
}
function set_actif(){
	$('.fa.fa-eye').unbind("click");
	$('.fa.fa-times').unbind("click");
	$(".fa.fa-eye").click(function(){
		if($(this).parent().hasClass('categorie_secondaire')){
			id=$(this).parent().attr("class").split(" ")[2];
		}else{
			id=$(this).parent().attr("id");
		}
		$(this).appelDynamique({"type":"set_visible", "idpb":id},"./retourAjax/receptionAjax.php", "notify");
		$(this).attr("class","fa fa-times");
		set_actif();
	});
	$(".fa.fa-times").click(function(){
		if($(this).parent().hasClass('categorie_secondaire')){
			id=$(this).parent().attr("class").split(" ")[2];
		}else{
			id=$(this).parent().attr("id");
		}
		$(this).appelDynamique({"type":"set_invisible", "idpb":id},"./retourAjax/receptionAjax.php", "notify");
		$(this).attr("class","fa fa-eye");
		set_actif();
	});
}
function ajout_problematique(){
	$('.addpb').unbind("click");
	$('.addpbsec').unbind("click");
	$('.addpb').click(function(){
		$(this).before('<div><span contenteditable >Nouvelle problématique</span><i class="fa fa-check ajout_pb"></i></div>');
		$(".ajout_pb").click(function(){
			item=$(this).parent().find("span").text();
			$(this).parent().prepend('<i class="fa fa-times"></i>');
			$('.ajout_pb').attr("class","fa fa-pencil-square-o");
			set_actif();
			modification_pb();
			$(this).appelDynamique({"type":"ajout_pb","nature":item},"./retourAjax/receptionAjax.php", "notify");
		})
	});
	$('.addpbsec').click(function(){
		$(this).before('<div><span contenteditable >Nouvelle problématique secondaire</span><i class="fa fa-check ajout_pb_sec"></i></div>');
		$(".ajout_pb_sec").click(function(){
			idpb=$(this).parent().parent().parent().find("input[name='idpb']").val();
			item=$(this).parent().find("span").text();
			$(this).parent().prepend('<i class="fa fa-times"></i>');
			$(this).attr("class","fa fa-pencil-square-o");
			$(this).parent().attr("class","categorie_secondaire "+idpb);
			set_actif();
			modification_pb();
			$(this).appelDynamique({"type":"ajout_pb_sec","id":idpb,"nature":item},"./retourAjax/receptionAjax.php", "notify");
		});
	})
}
function afficheGraphCamenbert(){
	$('#camenbertProblematique').toggle(500);

}

function afficheGraphDuree(){
	$('#graphAppelsTemps').toggle(500);

}